package com.pzh.guli.service.vod.controller;

import com.aliyuncs.exceptions.ClientException;
import com.pzh.guli.service.base.result.Result;
import com.pzh.guli.service.vod.service.VodVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/admin/vod/media")
public class VodController {
    @Autowired
    private VodVideoService vodVideoService;
    @PostMapping("upload")
    public Result uploadVideo(@RequestParam("file")MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String s = vodVideoService.uploadVideo(inputStream, originalFilename);
        return Result.ok().data("videoId",s).data("filename",originalFilename);
    }
    //删除一个video
    @DeleteMapping("delete/{id}")
    public Result removeOneVideoById(@PathVariable String id){
        try {
            vodVideoService.removeVideo(id);
            return Result.ok().message("云视频删除成功");
        } catch (ClientException e) {
            return Result.error().message("云视频删除失败");
        }
    }
    //删除多个video
    @DeleteMapping("remove")
    public Result removeMoreVideoByIds(@RequestBody List<String> ids){
        try {
            vodVideoService.removeVideoByList(ids);
            return Result.ok().message("云视频批量删除成功");
        } catch (ClientException e) {
            return Result.error().message("云视频批量删除失败");
        }
    }
}
